package com.yang.doctorrecruitment.dao;

import com.yang.doctorrecruitment.dto.Apply;
import com.yang.doctorrecruitment.dto.Message;
import com.yang.doctorrecruitment.dto.UserLogin;
import com.yang.doctorrecruitment.utils.DBManager;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class MessageDAO {
    private static Connection conn;
    public static boolean InsertMessage(Message message){
        conn= DBManager.getconnection();
        ResultSet res=null;
        PreparedStatement statement=null;
        String sql="insert into message values(?,?,?,?)";

        try{
            statement=conn.prepareStatement(sql);
            statement.setString(1,message.getUserID());
            statement.setString(2,message.getToUserID());
            statement.setString(3,message.getMessage());
            statement.setString(4,message.getTime());
            int count=statement.executeUpdate();
            if(count>=1){
                return true;
            }

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            DBManager.closeAll(conn,statement,res);
        }
        return false;
    }
    public static List<Message> selectToUserID(String UserID){
        //收件箱
        conn= DBManager.getconnection();
        ResultSet res=null;
        PreparedStatement statement=null;
        List<Message> list=new ArrayList<>();
        String sql="select * from message where ToUserID=?";

        try{
            statement=conn.prepareStatement(sql);
            statement.setString(1,UserID);
            res=statement.executeQuery();
            while(res.next()){
                Message message=new Message();
                message.setUserID(res.getString("UserID"));
                message.setToUserID(res.getString("ToUserID"));
                message.setMessage(res.getString("Message"));
                message.setTime(res.getString("RTime"));
                list.add(message);
            }
           return list;
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            DBManager.closeAll(conn,statement,res);
        }
        return null;
    }
    public static List<UserLogin> selectUserName(String UserID){
        //收件箱
        conn= DBManager.getconnection();
        ResultSet res=null;
        PreparedStatement statement=null;
        List<UserLogin> list=new ArrayList<>();
        String sql="select * from userlogin where" +
                " UserID in(select UserID from message where ToUserID=?)";
        try{
            statement=conn.prepareStatement(sql);
            statement.setString(1,UserID);
            res=statement.executeQuery();
            while(res.next()){
               UserLogin user=new UserLogin();
               user.setUserName(res.getString("UserName"));
               user.setUserID(res.getString("UserID"));
               user.setUserKind(res.getString("UserKind"));
                list.add(user);
            }
            return list;
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            DBManager.closeAll(conn,statement,res);
        }
        return null;
    }
    public static List<Message> selectUserID(String UserID){
        //收件箱
        conn= DBManager.getconnection();
        ResultSet res=null;
        PreparedStatement statement=null;
        List<Message> list=new ArrayList<>();
        String sql="select * from message where UserID=?";

        try{
            statement=conn.prepareStatement(sql);
            statement.setString(1,UserID);
            res=statement.executeQuery();
            while(res.next()){
                Message message=new Message();
                message.setUserID(res.getString("UserID"));
                message.setToUserID(res.getString("ToUserID"));
                message.setMessage(res.getString("Message"));
                message.setTime(res.getString("RTime"));
                list.add(message);
            }
            return list;
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            DBManager.closeAll(conn,statement,res);
        }
        return null;
    }

    public static List<UserLogin> selectReceiveUserName(String UserID){
        //收件箱
        conn= DBManager.getconnection();
        ResultSet res=null;
        PreparedStatement statement=null;
        List<UserLogin> list=new ArrayList<>();
        String sql="select * from userlogin where" +
                " UserID in(select ToUserID from message where UserID=?)";
        try{
            statement=conn.prepareStatement(sql);
            statement.setString(1,UserID);
            res=statement.executeQuery();
            while(res.next()){
                UserLogin user=new UserLogin();
                user.setUserName(res.getString("UserName"));
                user.setUserID(res.getString("UserID"));
                user.setUserKind(res.getString("UserKind"));
                list.add(user);
            }
            return list;
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            DBManager.closeAll(conn,statement,res);
        }
        return null;
    }

    public static boolean hasReply(String UserID,String ToUserID){
        //收件箱
        conn= DBManager.getconnection();
        ResultSet res=null;
        PreparedStatement statement=null;
        List<UserLogin> list=new ArrayList<>();
        String sql="select * from message where UserID=? and ToUserID=?";
        try{
            statement=conn.prepareStatement(sql);
            statement.setString(1,UserID);
            statement.setString(2,ToUserID);
            res=statement.executeQuery();
            if(res.next()){
                return true;
            }

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            DBManager.closeAll(conn,statement,res);
        }
        return false;
    }

    public static boolean updataReply(Message message){
        //收件箱
        conn= DBManager.getconnection();
        ResultSet res=null;
        PreparedStatement statement=null;
        List<UserLogin> list=new ArrayList<>();
        String sql="update message set Message=?,RTime=? where UserID=? and ToUserID=?";
        try{
            statement=conn.prepareStatement(sql);
            statement.setString(1,message.getMessage());
            statement.setString(2,message.getTime());
            statement.setString(3,message.getUserID());
            statement.setString(4,message.getToUserID());
            int count=statement.executeUpdate();
            if(count>=1){
                return true;
            }

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            DBManager.closeAll(conn,statement,res);
        }
        return false;
    }
}
